-------------------------------------------------------
-- 天梯争霸 跨服逻辑
-------------------------------------------------------

local LuaMongo = _G.lua_mongo
local DB = require("common.DB") 
local Util = require("common.Util")
local Lang = require("common.Lang")
local Log = require("common.Log")
local InnerMsg = require("core.InnerMsg")
local BillboardDB = require("billboard.BillboardDB")
local BillboardDefine = require("billboard.BillboardDefine")
local MiddleLogic = require("middle.MiddleLogic")
local MiddleManager = require("middle.MiddleManager")
local JjcLadderLogic = require("jjcLadder.JjcLadderLogic")
local RoleLogic = require("role.RoleLogic")

local QueryByUuid = {_id = nil}
local FieldSort = {rolebase = 1}
local FieldLogDel = {cnt = 1}
local FieldCombatLog = {time=1,attacker=1,defender=1,isWin=1,jjcLadder=1}
local DBUpdateField = {}								-- 更新域

local LASTTOP_CNT = 100
local BOARD_MAX_CNT = 50
local LOG_MAX_CNT = 30
local RANDOM_TARGET_CNT = 5

function saveTop(uuid2beMobaiCnt)
	local commonDB = JjcLadderLogic.getCommonDB()	
	local rank2uuid = commonDB.rank2uuid
	local lastTopList = commonDB.lastTopList
	-- 保存上一次前x信息
	commonDB.lastTopList = {}
	commonDB.lastUuid2Rank = {}
	for i = 1, LASTTOP_CNT do
		local uuid = rank2uuid and rank2uuid[i]
		local playerData = uuid and getPlayerData(uuid)
		if not playerData then break end
		
		commonDB.lastUuid2Rank[uuid] = i 
		commonDB.lastTopList[i] = playerData.rolebase
		if uuid2beMobaiCnt and uuid2beMobaiCnt[uuid] then
			playerData.rolebase.beMobaiCnt = uuid2beMobaiCnt[uuid]
		end	
	end
	JjcLadderLogic.saveCommonDB(commonDB)
end

-- 重置活动数据
function resetDB()
	local commonDB = JjcLadderLogic.getCommonDB()	
	commonDB.uuid2rank = nil 
	commonDB.rank2uuid = nil 
	commonDB.uuid2logs = nil 
	commonDB.logReds = nil

	commonDB.cleanTime = JjcLadderLogic.getCleanUpTime()
	-- 保存上一次前x信息
	--[[
	commonDB.lastTopList = {}
	commonDB.lastUuid2Rank = {}
	for i = 1, LASTTOP_CNT do
		local uuid = rank2uuid and rank2uuid[i]
		local playerData = uuid and getPlayerData(uuid)
		if not playerData then break end
		
		commonDB.lastUuid2Rank[uuid] = i 
		commonDB.lastTopList[i] = playerData.rolebase
		if uuid2beMobaiCnt and uuid2beMobaiCnt[uuid] then
			playerData.rolebase.beMobaiCnt = uuid2beMobaiCnt[uuid]
		end	
	end
	]]

	JjcLadderLogic.saveCommonDB(commonDB)

	LuaMongo.remove(DB.db_jjcLadder)
	LuaMongo.remove(DB.db_jjcLadder_record)
end

-- 获取名次 0表示未上榜
function getRank(uuid)
	local commonDB = JjcLadderLogic.getCommonDB()	
	if not commonDB.uuid2rank then
		return 0
	end
	return commonDB.uuid2rank[uuid] or 0
end

function getLastRank(uuid)
	local commonDB = JjcLadderLogic.getCommonDB()	
	if not commonDB.lastUuid2Rank then
		return 0
	end
	return commonDB.lastUuid2Rank[uuid] or 0
end

-- 获取参赛者信息
function getPlayerData(uuid, field)
	if not uuid then return end
	QueryByUuid._id = uuid
	LuaMongo.find(DB.db_jjcLadder, QueryByUuid, field)
	local playerData = {}
	return LuaMongo.next(playerData) and playerData
end

-- 保存/更新参赛者信息
function updatePlayerData(playerData)
	local oldData = getPlayerData(playerData._id)
	if not oldData then
		-- 首次收到，插入数据
		local commonDB = JjcLadderLogic.getCommonDB()		
		commonDB.rank2uuid = commonDB.rank2uuid or {}
		commonDB.uuid2rank = commonDB.uuid2rank or {}
		local rank = #commonDB.rank2uuid + 1
		commonDB.rank2uuid[rank] = playerData._id
		commonDB.uuid2rank[playerData._id] = rank
		JjcLadderLogic.saveCommonDB(commonDB)
		LuaMongo.insert(DB.db_jjcLadder, playerData)
	else
		-- 非首次，更新数据
		QueryByUuid._id = playerData._id
		LuaMongo.update(DB.db_jjcLadder, QueryByUuid, playerData)
	end	
end

-- 排序比较函数
local function cmpRank(a, b)
	if a.zhandouli ~= b.zhandouli then
		return a.zhandouli > b.zhandouli
	end
	return a.lv > b.lv
end

-- 按照战力重新排序
function sortRank()
	local commonDB = JjcLadderLogic.getCommonDB()	
	commonDB.rank2uuid = {}	
	commonDB.uuid2rank = {}

	local list = {}
	LuaMongo.find(DB.db_jjcLadder, nil, FieldSort)
	while true do
		local playerData = {}
		if not LuaMongo.next(playerData) then
			break
		end
		list[#list + 1] = playerData.rolebase
	end

	if #list > 1 then
		table.sort(list, cmpRank)
	end
	for rank = 1, #list do
		local uuid = list[rank].uuid
		commonDB.rank2uuid[rank] = uuid
		commonDB.uuid2rank[uuid] = rank
	end
	JjcLadderLogic.saveCommonDB(commonDB)
end

-- 获取可膜拜的人数
function getMobaiCnt()
	local commonDB = JjcLadderLogic.getCommonDB()
	if not commonDB.lastTopList then
		return 0
	end
	return math.min(#commonDB.lastTopList, JjcLadderLogic.MOBAI_PLAYER_CNT)
end

-- 是否有新纪录
function hasLogRed(uuid)
	local commonDB = JjcLadderLogic.getCommonDB()
	if not commonDB.logReds then
		return 
	end
	return commonDB.logReds[uuid]
end

-- 刷新纪录红点
function setLogRed(commonDB, uuid, flag)
	if not commonDB.logReds then
		commonDB.logReds = {} 
	end
	if commonDB.logReds[uuid] == flag then
		return
	end
	if flag == false then
		if commonDB.logReds[uuid] then
			commonDB.logReds[uuid] = nil
		end		
	else
		commonDB.logReds[uuid] = flag
	end
	
	return true
end

function getCombatInfo(videoUuid, field)
	if not videoUuid then return end
	QueryByUuid._id = videoUuid 
	LuaMongo.find(DB.db_jjcLadder_record, QueryByUuid, field)
	local combatInfo = {}
	return LuaMongo.next(combatInfo) and combatInfo
end

-- 
function delLog(videoUuid)
	local combatInfo = getCombatInfo(videoUuid, FieldLogDel)
	if not combatInfo then return end
	combatInfo.cnt = combatInfo.cnt - 1
	if combatInfo.cnt < 1 then
		QueryByUuid._id = videoUuid
		LuaMongo.remove(DB.db_jjcLadder_record, QueryByUuid)
	else
		QueryByUuid._id = videoUuid
		DBUpdateField["$set"] = combatInfo
		LuaMongo.update(DB.db_jjcLadder_record, QueryByUuid, combatInfo)
	end
end

-- 添加记录
function addLog(commonDB, combatInfo, uuid, targetUuid)
	combatInfo.cnt = 2
	LuaMongo.insert(DB.db_jjcLadder_record, combatInfo)

	commonDB.uuid2logs = commonDB.uuid2logs or {}
	commonDB.uuid2logs[uuid] = commonDB.uuid2logs[uuid] or {}
	commonDB.uuid2logs[targetUuid] = commonDB.uuid2logs[targetUuid] or {}
	local logs = commonDB.uuid2logs[uuid]
	local targetLogs = commonDB.uuid2logs[targetUuid]
	table.insert(logs, 1, combatInfo._id)
	table.insert(targetLogs, 1, combatInfo._id)

	for i = LOG_MAX_CNT + 1, #logs do
		delLog(logs[i])
		logs[i] = nil	
	end
	for i = LOG_MAX_CNT + 1, #targetLogs do
		delLog(targetLogs[i])
		targetLogs[i] = nil		
	end
end

-- 随机对手
local TEMP_RANDOM_LIST = {}
local function randomRank(minRank, maxRank, noRanks)
	local commonDB = JjcLadderLogic.getCommonDB()
	local cnt = 0
	for rank = minRank, maxRank do
		if (not noRanks[rank]) and 
			commonDB.rank2uuid[rank] then
			cnt = cnt + 1
			TEMP_RANDOM_LIST[cnt] = rank
		end
	end
	if cnt < 1 then return end
	local r = math.random(1, cnt)
	local rank = TEMP_RANDOM_LIST[r]
	local uuid = commonDB.rank2uuid[rank]
	return rank, uuid
end

-- 根据名次随机x个对手
local function randomTargetList(myRank)
	local targetList = {}
	if myRank < 1 then -- 没有参加资格，固定显示前5名
		local commonDB = JjcLadderLogic.getCommonDB()
		if not commonDB or not commonDB.rank2uuid then
			return targetList
		end
		for i = 1, RANDOM_TARGET_CNT do
			local uuid = commonDB.rank2uuid[i]
			local playerData = getPlayerData(uuid)
			local rolebase = playerData and playerData.rolebase
			if rolebase then
				rolebase.rank = i
				local list = {}
				list.roleBase = Util.copyTable(rolebase)
				list.objList = Util.copyTable(playerData.objList)
				targetList[#targetList + 1] = list
			end
		end
		return targetList
	end
	-- a.自身排名1000≤A，则每次从排名[A-100,A-50]的库中随机抽取5个，排名顺序展示在界面上
	-- b.自身排名200≤A＜1000，则每次从排名[A-80,A-30]的库中随机抽取5个，排名顺序展示在界面上
	-- c.自身排名100＜A＜200，则每次从排名[A-30,A-10]的库中随机抽取5个，排名顺序展示在界面上
	-- d.自身排名20<A≤99，则每次从排名[A-15,A-5]的库中随机抽取5个，排名顺序展示在界面上
	-- e.自身排名6≤A≤20，则从3~20名中随机取
	-- f.自身排名A≤6，则从1~6名中随机取
	local minRank, maxRank 
	if myRank >= 1000 then
		minRank = myRank - 100
		maxRank = myRank - 50
	elseif myRank >= 200 then
		minRank = myRank - 80
		maxRank = myRank - 30
	elseif myRank > 100 then
		minRank = myRank - 30
		maxRank = myRank - 10
	elseif myRank >= 20 then
		minRank = myRank - 15
		maxRank = myRank - 5
	elseif myRank > 6 then
		minRank = 3
		maxRank = 20
	else
		minRank = 1
		maxRank = 6		
	end

	local noRanks = {}
	noRanks[myRank] = true
	for i = 1, RANDOM_TARGET_CNT do
		local rank, uuid = randomRank(minRank, maxRank, noRanks)
		if rank and uuid then
			local playerData = getPlayerData(uuid)
			noRanks[rank] = true
			local rolebase = playerData and playerData.rolebase
			if rolebase then
				rolebase.rank = rank
				local list = {}
				list.roleBase = Util.copyTable(rolebase)
				list.objList = Util.copyTable(playerData.objList)
				targetList[#targetList + 1] = list
			end
		end
	end
	return targetList
end

-------------------------------------- middle -------------------------------------------
-- 主界面查询
function LW_JJC_LADDER_QUERY(fd, uuid)
	local playerData = getPlayerData(uuid)
	local myRank = getRank(uuid)
	local msgInner = InnerMsg.wl.WL_JJC_LADDER_QUERY
	msgInner.uuid = uuid
	-- 随机对手
	msgInner.targetList = randomTargetList(myRank)
	msgInner.mobaiCnt = getMobaiCnt()
	msgInner.logRed = hasLogRed(uuid) and 1 or 0
	msgInner.myRank = myRank
	InnerMsg.sendMsg(fd, msgInner)
end

-- 数据同步
function LW_JJC_LADDER_SENDDATA(fd, player)
	updatePlayerData(player)
end

-- 膜拜信息
function LW_JJC_LADDER_MOBAI_QUERY(fd, uuid)
	local commonDB = JjcLadderLogic.getCommonDB()
	local msgInner = InnerMsg.wl.WL_JJC_LADDER_MOBAI_QUERY
	msgInner.uuid = uuid
	msgInner.list = msgInner.list or {}
	Util.cleanTable(msgInner.list)
	for i = 1, JjcLadderLogic.MOBAI_PLAYER_CNT do
		local rolebase = commonDB.lastTopList and commonDB.lastTopList[i]
		if not rolebase then break end

		msgInner.list[i] = rolebase
	end
	InnerMsg.sendMsg(fd, msgInner)
end

-- 膜拜点赞
function LW_JJC_LADDER_MOBAI(fd, uuid, rank)
	local commonDB = JjcLadderLogic.getCommonDB()
	local rolebase = commonDB.lastTopList and commonDB.lastTopList[rank]
	if not rolebase then
		return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.JJC_LADDER_MOBAI_ERR_NONE)
	end

	rolebase.beMobaiCnt = (rolebase.beMobaiCnt or 0) + 1
	JjcLadderLogic.saveCommonDB(commonDB)

	local msgInner = InnerMsg.wl.WL_JJC_LADDER_MOBAI
	msgInner.uuid = uuid
	msgInner.rank = rank 
	InnerMsg.sendMsg(fd, msgInner)
	LW_JJC_LADDER_MOBAI_QUERY(fd, uuid)
--	LW_JJC_LADDER_QUERY(fd, uuid)

	-- 被膜拜的人同步次数
	local toFd = MiddleManager.getFDBySvrIndex(rolebase.svrIndex)
	if toFd then
		local msgInner2 = InnerMsg.wl.WL_JJC_LADDER_BEMOBAICNT
		msgInner2.uuid = rolebase.uuid
		msgInner2.beMobaiCnt = rolebase.beMobaiCnt
		InnerMsg.sendMsg(toFd, msgInner2)
	end
end

-- 查看名次
function LW_JJC_LADDER_RANK(fd, uuid)
	local msgInner = InnerMsg.wl.WL_JJC_LADDER_RANK
	msgInner.uuid = uuid
	if JjcLadderLogic.getState() == JjcLadderLogic.STATE_ACT_START then
		local myRank = getRank(uuid)
		msgInner.isLast = 0
		msgInner.rank = myRank		
	else
		msgInner.isLast = 1
		msgInner.rank = getLastRank(uuid)
	end
	InnerMsg.sendMsg(fd, msgInner)
end

local function makeBoardRankData(rankData, rolebase)
	rankData.roleBase = Util.copyTable(rolebase)
	rankData.uuid = rolebase.uuid
	rankData.value1 = rolebase.zhandouli or 0
	if rolebase.name and rolebase.svrName then
		rankData.roleBase.name = "[" .. rolebase.svrName .. "]" .. rolebase.name
	end
end

-- 
function getBoard()
	local billboard = BillboardDB.db[BillboardDefine.TYPE_JJCLADDER_M]
	if not billboard then return end

	local nowTime = os.time()
	if billboard.lastSortTime == nowTime then
		return billboard
	end
	billboard.lastSortTime = nowTime

	local commonDB = JjcLadderLogic.getCommonDB()
	local rank2uuid = commonDB.rank2uuid

	Util.cleanTable(billboard.uuid2rank)
	Util.cleanTable(billboard.rank2data)
	if JjcLadderLogic.getState() == JjcLadderLogic.STATE_ACT_START then
		if rank2uuid then
			for rank, uuid in ipairs(rank2uuid) do
				billboard.uuid2rank[uuid] = rank
				local playerData = getPlayerData(uuid)
				if not playerData then break end 
				local rankData = BillboardDB.initRankData()
				billboard.rank2data[rank] = rankData
				makeBoardRankData(rankData, playerData.rolebase)
			end
		end
	else
		local lastTopListLen = commonDB.lastTopList and #commonDB.lastTopList or 0
		for rank = 1, lastTopListLen do
			local rolebase = commonDB.lastTopList[rank]
			if not rolebase then break end

			billboard.uuid2rank[rolebase.uuid] = rank
			local rankData = BillboardDB.initRankData()
			billboard.rank2data[rank] = rankData
			makeBoardRankData(rankData, rolebase)
		end
	end
	return billboard
end

-- 排行榜
function LW_JJC_LADDER_BOARD(fd, uuid)
	local commonDB = JjcLadderLogic.getCommonDB()
	local rank2uuid = commonDB.rank2uuid
	local msgInner = InnerMsg.wl.WL_JJC_LADDER_BOARD
	msgInner.uuid = uuid 
	msgInner.list = msgInner.list or {}
	Util.cleanTable(msgInner.list)

	if JjcLadderLogic.getState() == JjcLadderLogic.STATE_ACT_START then
		-- 活动开始，活动排行
		for i = 1, BOARD_MAX_CNT do
			local tuuid = rank2uuid and rank2uuid[i]
			local playerData = tuuid and getPlayerData(tuuid)
			if not playerData then break end

			msgInner.list[i] = playerData.rolebase
		end
		local myPlayerData = getPlayerData(uuid)
		msgInner.myData = myPlayerData and myPlayerData.rolebase or {}
		msgInner.myData.rank = getRank(uuid)
		msgInner.isLast = 0
	else 
		-- 其它时候，上一期排行
		if commonDB.lastTopList then
			for i = 1, BOARD_MAX_CNT do
				local rolebase = commonDB.lastTopList[i]
				if not rolebase then break end
				msgInner.list[i] = rolebase
				if rolebase.uuid == uuid then
					msgInner.myData = rolebase
					msgInner.myData.rank = i 
				end
			end
		else
			msgInner.myData = {}
			msgInner.myData.rank = 0
		end	
		msgInner.isLast = 1
	end
	InnerMsg.sendMsg(fd, msgInner)
end

-- 战斗请求
function LW_JJC_LADDER_COMBAT_ASK(fd, uuid, targetUuid, targetRank)
	-- 活动结束就拜拜
	if JjcLadderLogic.getState() ~= JjcLadderLogic.STATE_ACT_START then
		return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.JJC_LADDER_ERR_TIME)
	end

	if not getPlayerData(uuid) then
		return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.JJC_LADDER_ERR_JOIN)
	end

	local targetPlayerData = getPlayerData(targetUuid)
	if not targetPlayerData then
		return LW_JJC_LADDER_QUERY(fd, uuid)
	end

	if getRank(targetUuid) ~= targetRank then
		return LW_JJC_LADDER_QUERY(fd, uuid)
	end

	local msgInner = InnerMsg.wl.WL_JJC_LADDER_COMBAT_ASK
	msgInner.uuid = uuid
	msgInner.rank = getRank(uuid) 
	msgInner.targetUuid = targetUuid
	msgInner.targetRank = targetRank
	msgInner.playerData = targetPlayerData
	InnerMsg.sendMsg(fd, msgInner)	
end

-- 战斗结果同步
function LW_JJC_LADDER_COMBAT_RESULT(fd, uuid, rank, targetUuid, targetRank, combatInfo)
    local selfData = getPlayerData(uuid)
	if not selfData then
		return MiddleLogic.sendWLBroadcast(fd, uuid, Lang.JJC_LADDER_ERR_JOIN)
	end

	local targetPlayerData = getPlayerData(targetUuid)
	if not targetPlayerData then
		return LW_JJC_LADDER_QUERY(fd, uuid)
	end

	if getRank(uuid) ~= rank or getRank(targetUuid) ~= targetRank then
		return LW_JJC_LADDER_QUERY(fd, uuid)
	end

	-- 结果处理
	local commonDB = JjcLadderLogic.getCommonDB()
	local newRank, newTargetRank = rank, targetRank
	if combatInfo.isActEnd then
	else
		if combatInfo.isWin and (rank > targetRank) then
			-- 胜利可以提升名次
			commonDB.uuid2rank[uuid] = targetRank
			commonDB.uuid2rank[targetUuid] = rank
			commonDB.rank2uuid[rank] = targetUuid
			commonDB.rank2uuid[targetRank] = uuid
			newRank, newTargetRank = targetRank, rank
		end
	end
	
	if combatInfo.isWin then
		setLogRed(commonDB, targetUuid,  true)
	end
	combatInfo.jjcLadder = {}
	combatInfo.jjcLadder.rank = rank 
	combatInfo.jjcLadder.targetRank = targetRank
	combatInfo.jjcLadder.newRank = newRank
	combatInfo.jjcLadder.newTargetRank = newTargetRank
	addLog(commonDB, combatInfo, uuid, targetUuid)

	JjcLadderLogic.saveCommonDB(commonDB)

	local msgInner = InnerMsg.wl.WL_JJC_LADDER_COMBAT_RESULT
	msgInner.uuid = uuid
	msgInner.rank = rank 
	msgInner.targetUuid = targetUuid
	msgInner.targetRank = targetRank
	InnerMsg.sendMsg(fd, msgInner)	
end

local function getLogContent(combatInfo, seeuuid)
	local jjcLadder = combatInfo.jjcLadder
	if combatInfo.defender.uuid == seeuuid then
		-- 防守方观看
		if combatInfo.isWin then
			if jjcLadder.targetRank ~= jjcLadder.newTargetRank then
				return Util.format(Lang.JJC_LADDER_LOG4, combatInfo.attacker.name, jjcLadder.targetRank, jjcLadder.newTargetRank)
			else
				return Util.format(Lang.JJC_LADDER_LOG5, combatInfo.attacker.name)
			end
		else
			return Util.format(Lang.JJC_LADDER_LOG6, combatInfo.attacker.name)
		end
	else
		-- 攻击方观看/其它情况
		if combatInfo.isWin then
			if jjcLadder.rank ~= jjcLadder.newRank then
				return Util.format(Lang.JJC_LADDER_LOG1, combatInfo.defender.name, jjcLadder.rank, jjcLadder.newRank)
			else
				return Util.format(Lang.JJC_LADDER_LOG2, combatInfo.defender.name)
			end
		else
			return Util.format(Lang.JJC_LADDER_LOG3, combatInfo.defender.name)
		end	
	end
end

local function fontLadderLog(net, combatInfo, seeuuid)
	net.time = combatInfo.time 
	net.isWin = combatInfo.isWin
	local jjcLadder = combatInfo.jjcLadder
	net.attacker = Util.copyTable(combatInfo.attacker)
	net.defender = Util.copyTable(combatInfo.defender)
	net.atkRank  = combatInfo.jjcLadder.rank
	net.defRank  = combatInfo.jjcLadder.targetRank
--	net.jjcLadder = Util.copyTable(combatInfo.jjcLadder)
end

local function sortGodLog(a, b)
	return a[2] > b[2]
end

function getGodFightLog(msgInner, commonDB)

	local logList ={}
	local haveList = {}
	local cnt = 0
	for rank, uuid in ipairs(commonDB.rank2uuid) do
		if rank > 20 then break end
		local logs = commonDB.uuid2logs and commonDB.uuid2logs[uuid]
		local logsLen = logs and #logs or 0
		logsLen = logsLen < 10 and logsLen or 10
		for i = 1, logsLen do
			local videoUuid = logs[i]
			if not haveList[videoUuid] then
				local combatInfo = getCombatInfo(videoUuid, FieldCombatLog)
				if combatInfo.jjcLadder and (combatInfo.jjcLadder.rank <= 20 or combatInfo.jjcLadder.targetRank <= 20 ) then
					cnt = cnt + 1
					logList[cnt] = {}
					logList[cnt][1] =videoUuid
					logList[cnt][2] = combatInfo.time
					haveList[videoUuid] = combatInfo.time
				end
			end			
		end	
	end

	if #logList > 0  then
		table.sort( logList, sortGodLog )
		for i = 1, 10 do
			if logList[i] and logList[i][1] then
				local combatInfo = getCombatInfo(logList[i][1], FieldCombatLog)
				local log = {}
				fontLadderLog(log, combatInfo)
				log.videoUuid = logList[i][1]
				msgInner.godLogs[#msgInner.godLogs + 1] = log
			end
			
		end
	end
end

-- 对战日志
function LW_JJC_LADDER_LOG(fd, uuid)
	local commonDB = JjcLadderLogic.getCommonDB()
	local logs = commonDB.uuid2logs and commonDB.uuid2logs[uuid]
	local logsLen = logs and #logs or 0
	local msgInner = InnerMsg.wl.WL_JJC_LADDER_LOG
	msgInner.uuid = uuid
	msgInner.logs = msgInner.logs or {}
	msgInner.godLogs = msgInner.godLogs or {}
	Util.cleanTable(msgInner.logs)
	Util.cleanTable(msgInner.godLogs)

	for i = 1, logsLen do
		local videoUuid = logs[i]
		local combatInfo = getCombatInfo(videoUuid, FieldCombatLog)
		if combatInfo then
			local log = {}
			fontLadderLog(log, combatInfo, uuid)
			log.videoUuid = videoUuid
			msgInner.logs[#msgInner.logs + 1] = log
		end
	end
	getGodFightLog(msgInner, commonDB)
	InnerMsg.sendMsg(fd, msgInner)

	setLogRed(commonDB, uuid,  false)
end

-- 战斗回放
function LW_JJC_LADDER_REPEAT_VIDEO(fd, uuid, videoUuid)
	local combatInfo = getCombatInfo(videoUuid)
	local msgInner = InnerMsg.wl.WL_JJC_LADDER_REPEAT_VIDEO
	msgInner.uuid = uuid
	msgInner.combatInfo = combatInfo or {}
	InnerMsg.sendMsg(fd, msgInner)
end

-- 同步可膜拜人数
function sendWLMobaiCnt(fd)
	local msgInner = InnerMsg.wl.WL_JJC_LADDER_MOBAI_CNT
	msgInner.mobaiCnt = getMobaiCnt()
	InnerMsg.sendMsg(fd, msgInner)
end

------------------------------------- 活动状态变化 ------------------------------------------
function onActReady()
	-- 活动准备开始 把上期前三 称号去除
	local commonDB = JjcLadderLogic.getCommonDB()
	print(" onActReady  middle !!! go top  chenghao del ")
	if commonDB.lastTopList then
		local svrList = {}
		local msgInner = InnerMsg.wl.WL_JJC_LADDER_CHENGHAO_DEL
		msgInner.list = msgInner.list or {}
		Util.cleanTable(msgInner.list)
		for i = 1, 3 do
			local rolebase = commonDB.lastTopList[i]
			if not rolebase then break end
			msgInner.list[rolebase.uuid] = {}
			msgInner.list[rolebase.uuid].svrIndex = rolebase.svrIndex
			msgInner.list[rolebase.uuid].rank = i
			svrList[rolebase.svrIndex] = 1
		end

		for svrIndex in pairs(svrList) do
			local toFd = MiddleManager.getFDBySvrIndex(svrIndex)
			InnerMsg.sendMsg(toFd, msgInner)
		end
	end
end

function onActStart()
	-- 活动开始，按照战力排序
	sortRank()
end

function onActEnd()
	-- 告知各个服结果
	local commonDB = JjcLadderLogic.getCommonDB()
	local svrIndex2ranks = {}
	if commonDB.rank2uuid then
		for rank, uuid in ipairs(commonDB.rank2uuid) do
			local playerData = getPlayerData(uuid, FieldSort)
			local svrIndex = playerData and playerData.rolebase and playerData.rolebase.svrIndex
			-- 日志
			--Log.write(Log.LOGID_DEBUG, "jjcLadder", rank, uuid, svrIndex)

			-- 按照服对结果进行分组，返还本服
			local lastSvrIndex = svrIndex and MiddleManager.getLastSvrIndex(svrIndex)
			if lastSvrIndex then
				svrIndex2ranks[lastSvrIndex] = svrIndex2ranks[lastSvrIndex] or {}
				svrIndex2ranks[lastSvrIndex][rank] = uuid
			end
		end
	end

	local msgResult = InnerMsg.wl.WL_JJC_LADDER_FINAL_RESULT
	local uuid2beMobaiCnt = {}
	local lastTopCnt = commonDB.lastTopList and #commonDB.lastTopList or 0
	for i = 1, lastTopCnt do
		local rolebase = commonDB.lastTopList[i]
		uuid2beMobaiCnt[rolebase.uuid] = rolebase.beMobaiCnt
	end
	-- 保存前3
	saveTop(uuid2beMobaiCnt)

	-- 同步结果
	for svrIndex, fd in pairs(MiddleManager.SVRINDEX_2_FD) do
		-- 排名结果
		msgResult.list = svrIndex2ranks[svrIndex] or {}
		InnerMsg.sendMsg(fd, msgResult)	

		sendWLMobaiCnt(fd)	
	end
end

function resetDbActEnd()
	-- 重置数据
	resetDB()
end
